package com.woniuxy.thread;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 创建线程的第二种方式
 *
 * @author 老谭 (<a href="http://www.woniuxy.com">蜗牛学苑</a>)
 *
 */

class Thread2 implements Runnable {

	@Override
	public void run() {
		Random r = new Random();
		for (int i = 0; i < 10; i++) {
			try {
				// 1-999
				TimeUnit.MILLISECONDS.sleep(r.nextInt(1000));
				System.out.println(Thread.currentThread().getName() + ":" + i);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

public class ThreadTest2 {
	public static void main(String[] args) {
		Thread2 thread2 = new Thread2();
		new Thread(thread2).start();
		Random r = new Random();
		for (int i = 0; i < 10; i++) {
			try {
				TimeUnit.MILLISECONDS.sleep(r.nextInt(1000));
				System.out.println(Thread.currentThread().getName() + ":" + i);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
